<?php

namespace app\common\helper;

use think\Db;

class MyHelper
{


    static function getArticleInfoByIdAbutBiaoyin($id)
    {
        $map['id'] = $id;
        $result = Db::table("zstp_article")->where($map)->find();

        $result['text'] = $result['content'];


        $result['content'] = str_replace(array("&nbsp;", " ", "\r"), '', $result['content']);

        $result['content'] = str_replace(array("\r\n"), "\n", $result['content']);

        $result['content'] = str_replace(";", ";\n", $result['content']);
        $result['content'] = str_replace("。", "。\n", $result['content']);


//
//        die();   // 去换行
        $data = Db::table("zstp_conf")->field("value")->where(array("type" => 4))->select();

        if (!empty($data)) {
            $serArray = array();
            $repArray = array();
            foreach ($data as $key => $value) {
                $serArray[] = $value['value'] . "\n";
                $repArray[] = $value['value'];
            }
            $result['content'] = str_replace($serArray, $repArray, $result['content']);

        }

        $data = Db::table("zstp_conf")->field("value")->where(array("type" => 5))->select();

        if (!empty($data)) {
            $serArray = array();
            $repArray = array();
            foreach ($data as $key => $value) {
                $serArray[] = $value['value'];
                $repArray[] = $value['value'] . "\n";
            }
            $result['content'] = str_replace($serArray, $repArray, $result['content']);
        }
        // 去换行 文字替换
        $datatiHuan = Db::table("zstp_conf")->field("value")->where(array("type" => 2))->select();

        if (!empty($datatiHuan)) {
            $serArrayTi = array();
            $repArrayTi = array();
            foreach ($datatiHuan as $key => $value) {
                $ex = explode(",", $value['value']);
                $serArrayTi[] = $ex[0];
                $repArrayTi[] = $ex[1];

            }

            $result['content'] = str_replace($serArrayTi, $repArrayTi, $result['content']);

        }


//        $result['content'] = automaticParagraphWrappingSplicing($result['content']);


        if (empty($result)) {

            return array("code" => 400, "data" => "", "message" => "这个数据没有");
        }
        return array("code" => 200, "data" => $result, "message" => "获取成功");
    }


    //模版 加只是抽取
    static function getArticleInfoById($id)
    {
        $map['id'] = $id;
        $result = Db::table("zstp_article")->where($map)->find();


        $result['content'] = str_replace(array(" ", "&nbsp;", " ", "\r"), '', $result['content']);

        $result['content'] = str_replace(array("\r\n"), "\n", $result['content']);

        $result['content'] = str_replace(";", ";\n", $result['content']);
        $result['content'] = str_replace("。", "。\n", $result['content']);


        $result['content'] = automaticParagraphWrappingSplicing($result['content']);


        if (empty($result)) {

            return array("code" => 400, "data" => "", "message" => "这个数据没有");
        }
        return array("code" => 200, "data" => $result, "message" => "获取成功");
    }

    /** 获取头实体
     * @param $headRegOrDic
     * @param $list
     * @param $headRadio
     * @param $headReg
     * @return void
     */
    static function getHeadEntity($headRegOrDic, $list, $headRadio, $headReg, $headDic, $headRegAndDic = "", $guding = "")
    {


        $headEntity = array();
        if ($headRegOrDic == 1) {
            // 走正则

            if (empty($headReg)) {

                return array("code" => 400, "data" => "", "message" => "填写正则");
            }
            foreach ($list as $key => $value) {
                $key = $key + 1;

                if ($headRadio == 1) {
                    $res = targetArea($value, $headReg);


                    if ($res ['code'] == 200) {
                        $headEntity[$key] = array($res ['data']);
                    }
                }
                if ($headRadio == 2) {
                    $res = multipleTargetArea($value, $headReg);
                    if ($res ['code'] == 200) {
                        $headEntity[$key] = $res ['data'];
                    } else {
                        return $res;
                    }
                }
            }
            return array("code" => 200, "data" => $headEntity, "message" => "");
        } elseif ($headRegOrDic == 2) {
            // 走字典
            //获取字典
            $map['is_delete'] = 0;
            $map['status'] = 1;
            $map['dictionary_type'] = $headDic;
            $dictionaryData = Db::table("zstp_dictionary")->field("id,dictionary_value")->where($map)->select();

            foreach ($list as $key => $value) {
                $key = $key + 1;
                $headEntity[$key] = array();
                $leg = 0;
                foreach ($dictionaryData as $k => $v) {
                    $res = strpos($value, $v['dictionary_value']);
                    if ($res) {
                        if ($headRadio == 1) {
                            $dictionaryValue = $v['dictionary_value'];
                            $releg = mb_strlen($dictionaryValue, 'utf-8');

                            if ($releg > $leg) {
                                $headEntity[$key][0] = $v['dictionary_value'];
                            }

                            $leg = $releg;
//                            var_dump($v['dictionary_value']);

                        } else {
                            $headEntity[$key][] = $v['dictionary_value'];
                        }


//                        if ($headRadio == 1) {
//                            break;
//                        }
                    }
                }

            }

            return array("code" => 200, "data" => $headEntity, "message" => "");
        } elseif ($headRegOrDic == 3) {


            // 校验正则

            if (empty($headRegAndDic)) {
                return array("code" => 400, "data" => "", "message" => "填写正则");
            }

            $count = substr_count($headRegAndDic, "字典");

            if ($count > 1) {
                return array("code" => 400, "data" => "", "message" => "仅支持单字典正则");
            }

            if ($count < 1) {
                return array("code" => 400, "data" => "", "message" => "正则中未出现字典");
            }

            if (empty($headDic)) {
                return array("code" => 400, "data" => "", "message" => "请选择字典");
            }
            //查字典数据
            $mapT['is_delete'] = 0;
            $mapT['status'] = 1;
            $mapT['dictionary_type'] = $headDic;
            $dictionaryData = Db::table("zstp_dictionary")->field("id,dictionary_value")->where($mapT)->select();

            $instance = new self();

            foreach ($list as $key => $value) {
                $key = $key + 1;
                $str = $value;
                $headEntity[$key] = $instance->regAndDic($dictionaryData, $headRegAndDic, $str, $headRadio, $instance);
            }


            return array("code" => 200, "data" => $headEntity, "message" => "");


        } else {
            if (empty($guding)) {

                return array("code" => 400, "data" => "", "message" => "填写固定字段");
            }


            foreach ($list as $key => $value) {
                $key = $key + 1;

                if ($headRadio == 1) {
                    $res = targetArea($value, $headReg);
                    if ($res ['code'] == 200) {
                        $headEntity[$key] = array($guding);
                    }
                }
                if ($headRadio == 2) {

                    $headEntity[$key] = array($guding);

                }
            }
            return array("code" => 200, "data" => $headEntity, "message" => "");


        }


    }

    static function regAndDicBiaozhu($dictionaryData, $headRegAndDic, $str, $instance)
    {
        $newArr = array();
        $arr = array();
        foreach ($dictionaryData as $key => $value) {
            $dictionary = $value['dictionary_value'];
            $newDictionary = str_replace("字典", $dictionary, $headRegAndDic);
            $res = targetArea($str, $newDictionary);
            if (!empty($res['data'])) {
                $arr[] = $res['data'];
            }
        }

        $newArr = "";
        $arr = array_unique($arr);
        $leg = 0;
        foreach ($arr as $kd => $vd) {

            $letR = mb_strlen($vd, 'utf-8');
            if ($leg == 0) {

                $newArr = $vd;
            }
            if ($letR < $leg) {
                $newArr = $vd;
            }

            $leg = $letR;

        }
        return $newArr;


    }


    static function regAndDic($dictionaryData, $headRegAndDic, $str, $headRadio, $instance)
    {
        $newArr = array();
        $arr = array();
        foreach ($dictionaryData as $key => $value) {
            $dictionary = $value['dictionary_value'];
            $newDictionary = str_replace("字典", $dictionary, $headRegAndDic);
            if ($headRadio == 1) {
                $res = targetArea($str, $newDictionary);
                if (!empty($res['data'])) {
                    $arr[] = $res['data'];
                }
            }
            if ($headRadio == 2) {
                $res = multipleTargetArea($str, $newDictionary);
                if (!empty($res['data'])) {

                    $arr[] = $res['data'];
                }
            }
        }

        if ($headRadio == 1) {
            $newArr = array();
            $arr = array_unique($arr);
            $leg = 0;
            foreach ($arr as $kd => $vd) {

                $letR = mb_strlen($vd, 'utf-8');
                if ($leg == 0) {

                    $newArr[0] = $vd;
                }
                if ($letR < $leg) {
                    $newArr[0] = $vd;
                }

                $leg = $letR;

            }
            return $newArr;
        }


        if ($headRadio == 2) {
            $newArrs = array();
            foreach ($arr as $key => $value) {
                foreach ($value as $kl => $vl) {
                    $newArr[] = $vl;
                }
            }

            $newArr = array_unique($newArr);

            foreach ($newArr as $key => $value) {
                $res = $instance->listStrpos($newArr, $value);

                if (!empty($res)) {
                    $newArrs = array_merge($newArrs, $res);
                }

            }

            if (empty($newArrs)) {
                return $newArr;
            }

            return $newArrs;

        }

    }


    function listStrpos($arr, $word)
    {
        $newArr = array();
        foreach ($arr as $key => $value) {
            if ($word == $value) {
                continue;
            }
            if (strpos($value, $word) !== false) {
                $newArr[] = $word;
            }
        }
        return $newArr;

    }


    public static function getMdzfcWweiZhi($arr, $mdzfc)
    {

        $newArr = [];
        foreach ($arr as $key => $value) {
            if ($value['str'] == " ") {
                continue;
            }
            $newArr[] = $value;
        }


        $chars = mb_str_split($mdzfc);
//        var_dump($chars);
        $num = count($chars);


        $newChars = array();
        foreach ($chars as $key => $value) {
//            var_dump($value);
//            $newChars=array();
            foreach ($newArr as $k => $v) {

                $str = $v['str'];
                // 第一命中
                if ($str == $value) {

                    $newChars[$key] = $v;
                    for ($i = 1; $i < $num; $i++) {
//                        var_dump($k + $i);
                        $next = $newArr[$k + $i];
//                        var_dump($next);
//                        die();

                        $nextText = $next["str"];
                        if ($nextText == $chars[$i]) {
                            $newChars[$key + $i] = $next;
                            if ($i == $num - 1) {
                                return $newChars;
                            }
                        } else {

                            $newChars = array();
                            break 1;
                        }

                    }


                }

            }

        }

        return false;
    }


    public static function dingWei($arr, $mdzfc, $xStartName, $xEndName, $yStartName, $yEndName, $xStartDs, $xEndDs, $yStartDs, $yEndDs)
    {


//        var_dump("xStartName:".$xStartName);
//        var_dump("xEndName:".$xEndName);
//        var_dump("yStartName:".$yStartName);
//        var_dump("yEndName:".$yEndName);
//        var_dump("xStartDs:".$xStartDs);
//        var_dump("xEndDs:".$xEndDs);
//        var_dump("yStartDs:".$yStartDs);

//        var_dump($arr);
//        var_dump($mdzfc);

        $contentArr = $arr;

        $data = self:: getMdzfcWweiZhi($arr, $mdzfc);


        if (empty($data)) {

            return array("code" => 400, "data" => "", "message" => "未找到");
        }


        //x轴起始点
        $xStart = 0;
        $position = mb_strpos($mdzfc, $xStartName, 0, 'UTF-8');

        if (!empty($xStartName)) {
            $xStart = $data[$position]['x'];
        }
        $xStart = $xStart . $xStartDs;


        $safeExpression = str_replace('w', $data[$position]['width'], $xStart);
        $safeExpression = str_replace('h', $data[$position]['height'], $safeExpression);
        $minX = 0;


        eval('$minX = ' . $safeExpression . ';');

//        var_dump("minX" . $minX);


        //x轴终点
        $xEnd = 0;

        $position = mb_strpos($mdzfc, $xEndName, 0, 'UTF-8');


        if (!empty($xEndName)) {
            $xEnd = $data[$position]['x'];
        }

        $xEnd = $xEnd . $xEndDs;

        $safeExpression = str_replace('w', $data[$position]['width'], $xEnd);

        $safeExpression = str_replace('h', $data[$position]['height'], $safeExpression);

        $maxX = 0;

        eval('$maxX = ' . $safeExpression . ';');

//        var_dump("maxX" . $maxX);


        //y轴起始点
        $yStart = 0;

        $position = mb_strpos($mdzfc, $yStartName, 0, 'UTF-8');

        if (!empty($yStartName)) {
            $yStart = $data[$position]['accumulated_y'];
        }

        $yStart = $yStart . $yStartDs;

        $safeExpression = str_replace('w', $data[$position]['width'], $yStart);

        $safeExpression = str_replace('h', $data[$position]['height'], $safeExpression);

        $minY = 0;

        eval('$minY = ' . $safeExpression . ';');

//        var_dump("minY" . $minY);


        $yEnd = 0;
        $position = mb_strpos($mdzfc, $yEndName, 0, 'UTF-8');
        if (!empty($yEndName)) {
            $yEnd = $data[$position]['accumulated_y'];
        }

        $yEnd = $yEnd . $yEndDs;
        $safeExpression = str_replace('w', $data[$position]['width'], $yEnd);
        $safeExpression = str_replace('h', $data[$position]['height'], $safeExpression);

        $maxY = 0;
        eval('$maxY = ' . $safeExpression . ';');


        $text = "";

        foreach ($contentArr as $key => $value) {
            $textX = $value['x'];
            $textY = $value['accumulated_y'];

            if ($textX >= $minX && $textX <= $maxX && $textY >= $minY && $textY <= $maxY) {
                $text = $text . $value['str'];
            }

        }

        return array("code" => 200, "data" => $text, "message" => "");

    }


    function strpos($str, $word)
    {
        if (strpos($str, $word) !== false) {
            return true;
        } else {
            return false;
        }
    }

    /** 标引
     * @param
     * @return void
     */
    static public function biaoyin($content, $type, $reg, $Dic, $headRegAndDic)
    {


        // 正则匹配
        if ($type == 1) {

            if (empty($reg)) {
                return array("code" => 400, "data" => "", "message" => "正则表达式为空");
            }


            $res = preg_match($reg, $content, $matches);

            if ($res != 0) {
                return array("code" => 200, "data" => $matches[1], "message" => "");
            }
        }
        //字典
        if ($type == 2) {
            //获取字典
            $map['is_delete'] = 0;
            $map['status'] = 1;
            $map['dictionary_type'] = $Dic;
            $dictionaryData = Db::table("zstp_dictionary")->field("id,dictionary_value")->where($map)->select();
            foreach ($dictionaryData as $k => $v) {
                $res = strpos($content, $v['dictionary_value']);
                if ($res != false) {

                    return array("code" => 200, "data" => $v['dictionary_value'], "message" => "");
                }
            }

            return array("code" => 400, "data" => "", "message" => "没有命中字典");
        }

        if ($type == 3) {

            // 校验正则

            if (empty($headRegAndDic)) {
                return array("code" => 400, "data" => "", "message" => "填写正则");
            }

            $count = substr_count($headRegAndDic, "字典");

            if ($count > 1) {
                return array("code" => 400, "data" => "", "message" => "仅支持单字典正则");
            }

            if ($count < 1) {
                return array("code" => 400, "data" => "", "message" => "正则中未出现字典");
            }

            if (empty($headRegAndDic)) {
                return array("code" => 400, "data" => "", "message" => "请选择字典");
            }
            //查字典数据
            $mapT['is_delete'] = 0;
            $mapT['status'] = 1;
            $mapT['dictionary_type'] = $Dic;
            $dictionaryData = Db::table("zstp_dictionary")->field("id,dictionary_value")->where($mapT)->select();

            $instance = new self();


            $headEntity = $instance->regAndDicBiaozhu($dictionaryData, $headRegAndDic, $content, $instance);

            return array("code" => 200, "data" => $headEntity, "message" => "");
        }


        return array("code" => 400, "data" => [], "message" => "");

    }


    function test($path)
    {

        $json = file_get_contents($path);

        // json 转成数组
        $dataArr = json_decode($json, true);

        if (empty($dataArr['content'])) {

            return array("code" => 400, "公章没有内容");
        }

        $contentArr = $dataArr['content'];


        $text = "";
        foreach ($contentArr as $key => $value) {

            $text = $text . $value['str'];;
        }

        $keyword = "全宗号";

        $length = mb_strlen($keyword, "utf-8");

        $mun = mb_strpos($text, $keyword);

        $data1 = $contentArr[$mun];

        $mun = $mun + ($length - 1);

        $data3 = $contentArr[$mun];

        $x1 = $data1['x'];
        $y = $data1['y'];


        $xmin = $x1 - (0.7 * $data1['width']);


        var_dump($mun);
        var_dump($data1);


    }

        //模版 加只是抽取
        static function getArticleInfo($content)
        {
      
    
    
            $content = str_replace(array(" ", "&nbsp;", " ", "\r"), '', $content);
    
            $content = str_replace(array("\r\n"), "\n", $content);
    
            $content = str_replace(";", ";\n",  $content);
            $content = str_replace("。", "。\n", $content);
    
    
    //
    //        die();
            $data = Db::table("zstp_conf")->field("value")->where(array("type" => 1))->select();
    
            if (!empty($data)) {
                $serArray = array();
                $repArray = array();
                foreach ($data as $key => $value) {
                    $serArray[] = $value['value'] . "\n";
                    $repArray[] = $value['value'];
                }
                $content = str_replace($serArray, $repArray,  $content);
    
            }
    
            $data = Db::table("zstp_conf")->field("value")->where(array("type" => 3))->select();
    
            if (!empty($data)) {
                $serArray = array();
                $repArray = array();
                foreach ($data as $key => $value) {
                    $serArray[] = $value['value'];
                    $repArray[] = $value['value'] . "\n";
                }
    
                $content = str_replace($serArray, $repArray, $content);
    
            }
    
    
            $datatiHuan = Db::table("zstp_conf")->field("value")->where(array("type" => 2, "is_delete" => 1, "status" => 1))->select();
    
    
            if (!empty($datatiHuan)) {
                $serArrayTi = array();
                $repArrayTi = array();
    
                foreach ($datatiHuan as $key => $value) {
                
                    $ex = explode(",", $value['value']);
                    $serArrayTi[] = $ex[0];
                    $repArrayTi[] = $ex[1];
    
                }
    
                $content= str_replace($serArrayTi, $repArrayTi,  $content);
    
    
            }
    
    
            $content = automaticParagraphWrappingSplicing( $content);
    
    
            if (empty($content)) {
    
                return array("code" => 400, "data" => "", "message" => "这个数据没有");
            }
            return array("code" => 200, "data" =>  $content, "message" => "获取成功");
        }


}